package jsu.xxy.coffee3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jsu.xxy.coffee3.Utils.JwtUtil;
import jsu.xxy.coffee3.Utils.MD5;
import jsu.xxy.coffee3.domain.Customer;
import jsu.xxy.coffee3.domain.Login;
import jsu.xxy.coffee3.service.CustomerService;
import jsu.xxy.coffee3.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 张从轩
* @description 针对表【customer(员工表)】的数据库操作Service实现
* @createDate 2023-06-03 11:04:47
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService{

    @Autowired
    CustomerMapper customerMapper;
    @Override
    public Login login(String phone, String password) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        List<Customer> list = customerMapper.selectList(queryWrapper);
        Login login =new Login();
        if(list.isEmpty()){
            LocalDateTime localDateTime = LocalDateTime.now();
            Customer customer = new Customer(phone, phone, MD5.getMD5(password),localDateTime,"女",0);
            customerMapper.add(customer);
            queryWrapper.eq("phone",phone).eq("password" , MD5.getMD5(password));
            List<Customer> lis = customerMapper.selectList(queryWrapper);
            login.setCustomer(lis.get(0));
            login.setToken(JwtUtil.creatToken(phone,0));
            login.setIs(0);
        }
        else {
            queryWrapper.eq("phone",phone).eq("password" , MD5.getMD5(password));
            List<Customer> list2 = customerMapper.selectList(queryWrapper);
            if(list2.isEmpty()){
                login.setIs(2);
//                login.setToken(JwtUtil.creatToken(phone,0));
            }
            else {
                login.setCustomer(list2.get(0));
                login.setIs(1);
                login.setToken(JwtUtil.creatToken(phone,0));
            }
        }
        return login;
    }

    @Override
    public Page<Customer> getAll(Integer pageNo, Integer pageSize) {
        Page<Customer> page = new Page<>(pageNo, pageSize);
        customerMapper.selectPage(page, null);
        return page;
    }

    @Override
    public Page<Customer> selectByName(String name, Integer pageNo, Integer pageSize) {
        Page<Customer> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("nickname",name);
        customerMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public Page<Customer> selectByPhone(String phone, Integer pageNo, Integer pageSize) {
        Page<Customer> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("phone",phone);
        customerMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public Page<Customer> selectById(Integer id, Integer pageNo, Integer pageSize) {
        Page<Customer> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",id);
        customerMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public boolean changeById(Customer customer) {
        customer.setPassword(MD5.getMD5(customer.getPassword()));
        return customerMapper.updateNoPassword(customer)>0;
    }
}




